const http = "https://ly.unohacha.com"
const imgUrl = "/images/"

//sign拼接
function sorts(obj) {
    var arr = new Array();
    var num = 0;
    for (var i in obj) {
        arr[num] = i;
        num++;
    }
    var sortArr = arr.sort();
    var arr = {};
    for (var i in sortArr) {
        arr[sortArr[i]] = obj[sortArr[i]];
    }
    var a = "";
    for (let i in arr) {
        a += i + "%$%" + arr[i] + "^#^";
    }
    var objs = a.slice(0,-3)
    return objs;
}
// post请求
function _post(data, success, fail) {
    wx.request({
        url: http,
        header: {
            'content-type': 'application/x-www-form-urlencoded',
        },
        method: 'POST',
        data: data,
        success(res) {
            success(res)
        },
        fail(err) {
            fail(err)
        }
    })
}

// 获取data-index 属性
function dataIndex(e) {
    //0index,1ID,2indexA(父级)
    let index = e.currentTarget.dataset.index,
        id = e.currentTarget.dataset.id,
        indexA = e.currentTarget.dataset.indexa,
        dataIndex = [index, id, indexA]

    return dataIndex;
}

// 获取input框里面所有的数据
function inputArr(arr, e) {
    let index = dataIndex(e)[0],
    temp_arr = arr,
    temp_val = e.detail.value
    temp_arr[index] = temp_val
    return temp_arr
}

// 输入为空
function ifEmpty(val) {
    if (!val || !val.replace(/\s+/g, "")) {
        return false
    } else {
        return true
    }
}

// 基本正则验证
function rexFn(str) {
    var rex1 = /^1\d{10}$/ // 手机的正则匹配
    var rex2 = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ // 身份证正则匹配
    var rex3 = /^[\u4e00-\u9fa5]+$/ // 汉字匹配
    var falgArr = []
    if (rex1.test(str)) {
        falgArr[0] = true
    } else if (rex2.test(str)) {
        falgArr[1] = true
    } else if (rex3.test(str)) {
        falgArr[2] = true
    }
    return falgArr
}

// 版本比较
function compareVersion(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    let len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
        v1.push('0')
    }
    while (v2.length < len) {
        v2.push('0')
    }
    for (let i = 0; i < len; i++) {
        let num1 = parseInt(v1[i])
        let num2 = parseInt(v2[i])

        if (num1 > num2) {
            return 1
        } else if (num1 < num2) {
            return -1
        }
    }

    return 0
}

// 返回
function goBack() {
    wx.navigateBack({
        delta: 1
    })
}

function forStyChange(e, arr) {
    let index = dataIndex(e)[0];
    for (let i in arr) {
        arr[i].flag = 0;
    }
    arr[index].flag = 1;
    return arr;
}
// 检测当前机型的长宽
const nowPhoneWH =()=>{
  var a = []
  var res = wx.getSystemInfoSync()
  a[0] = res.windowWidth;
  a[1] = res.windowHeight;
  return a
}
module.exports = {
    http,
    imgUrl,
    _post,
    dataIndex,
    inputArr,
    ifEmpty,
    rexFn,
    compareVersion,
    goBack,
    forStyChange,
    nowPhoneWH,
    sorts
}